/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ecom.ejb;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateful;
import javax.ejb.Remote;

/**
 *
 * @author François
 */
@Stateful
@Remote(EuroConverterRemote.class)
public class EuroConverter implements EuroConverterRemote {

    @Override
    public double convertToEuro(double amount, Devise devise) {
	double retour = 0.0;

	retour = floor(amount / devise.getTaux(), 2);

	return retour;
    }

    @Override
    public double convertFromEuro(double amount, Devise devise) {
	double retour = 0.0;

	retour = floor(amount * devise.getTaux(), 2);

	return retour;
    }

    @Override
    public double convertCurrency(double amount, Devise deviseSource,
	    Devise deviseCible) {
	double retour = 0.0;

	double montantEuro = this.convertToEuro(amount, deviseSource);
	retour = this.convertFromEuro(montantEuro, deviseCible);

	return retour;
    }

    @Override
    public List<String> getSymbolesDevises() {
	List<String> retour = new ArrayList<String>();

	for (Devise devise : Devise.values()) {
	    retour.add(devise.getSymbole());
	}

	return retour;
    }

    @Override
    public Devise getDeviseFromSymbole(String symbole) {
	Devise retour = null;

	for (Devise dev : EuroConverterRemote.Devise.values()) {
	    if (dev.getSymbole().equals(symbole)) {
		retour = dev;
	    }
	}

	return retour;
    }

    public static double floor(double a, int n) {
	double p = Math.pow(10.0, n);
	return Math.floor((a * p) + 0.5) / p;
    }
}
